// pages/addINote/addINote.js
var util = require('../../utils/util.js');

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    jilurenID: wx.getStorageSync("openid"),
    jiluren: (app.globalData.userInfo == undefined || app.globalData.userInfo == {} || app.globalData.userInfo == "") ? "" : app.globalData.userInfo.nickName,
    date: util.formatDate(new Date()),
    lunar: ''
  },

  onConfirm: function () {

    //检查金额是否数字
    var that = this;

    var postObj = {
      "ID": Date.parse(new Date()) / 1000,
      "CATEGORY": '',
      "CONTENT": this.data.content,
      "CREATOR_ID": this.data.jilurenID,
      "CREATOR": (app.globalData.userInfo == undefined || app.globalData.userInfo == {} || app.globalData.userInfo == "") ? "" : app.globalData.userInfo.nickName,
      "RECORD_TIME": this.data.date,
      "LUNAR": this.data.lunar
    };
    console.log(JSON.stringify(postObj));
    wx.request({
      url: wx.getStorageSync("sURL"),
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        url: '/addNote',
        postdata: JSON.stringify(postObj)
      },
      method: 'POST',
      success: res => {
        console.log(res);
        wx.showToast({
          title: '请等待',
          icon: 'none',
          duration: 500,
          mask: true,
          success: function () {
            setTimeout(function () {
              //要延时执行的代码
              wx.reLaunch({
                url: '../iDiary/iDiary'
              });
            }, 500) //延迟时间
          }
        });
      },
      fail: res => {
        console.log(res);
        wx.showToast({
          title: '添加失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  onCancel: function () {
    // wx.navigateBack();
    setTimeout(function () {
      //要延时执行的代码
      wx.reLaunch({
        url: '../iDiary/iDiary'
      });
    }, 500) //延迟时间
  },

  jlsj_bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      date: e.detail.value
    })
  },

  input_content: function (e) {
    this.setData({
      content: e.detail.value
    });
  },

  input_lunar: function (e) {
    this.setData({
      lunar: e.detail.value
    });
  },

  input_jiluren: function (e) {
    this.setData({
      jiluren: e.detail.value
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      jiluren: (app.globalData.userInfo == undefined || app.globalData.userInfo == {} || app.globalData.userInfo == "") ? "" : app.globalData.userInfo.nickName
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})